<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>菜鸟拖拽</title>
    <style>
        .dropTarget{
            float:left;
            width:100px;
            height:100px;
            border: 1px solid red;
            margin: 15px;
        }
    </style>
</head>
<body>
    <div class="dropTarget" ondrop="drop(event)" ondragover="allowDrop(event)">
        <p id="dragtarget" draggable="true" ondragstart="dragStart(event)" ondragend="dragEnd(event)">拖动我!</p>
    </div>
    <div class="dropTarget" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div class="dropTarget" style="background: #bfa;"></div>
    <div class="dropTarget" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div class="dropTarget" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <script>
        function dragStart(event) {
            console.log(event.dataTransfer)
            event.dataTransfer.setData("Text", event.target.id)
        }
        function dragEnd(event) {
            console.log('end')
        }
        function allowDrop(event) {
            event.preventDefault()
        }

        function drop(event) {
            event.preventDefault()
            var data = event.dataTransfer.getData("Text")
            event.target.appendChild(document.getElementById(data))
        }
    </script>
</body>
</html>

